package com.graduationpractice.dbot.controller;


import com.graduationpractice.dbot.biz.IOperationlogService;
import com.graduationpractice.dbot.pojo.Operationlog;
import com.graduationpractice.dbot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-03-24
 */
@Controller
@RequestMapping("/usercreatetable")
public class UsercreatetableController {

    @Autowired
    IOperationlogService iOperationlogService;

    /*展示表*/
    @RequestMapping("showtable")
    public String showTable(String schema, HttpSession session) throws SQLException {
        //获取连接
        Connection connection=(Connection) session.getAttribute("connection");
        //获取数据库元数据
        DatabaseMetaData databaseMetaData=connection.getMetaData();
        //存储表名
        List<String> table_list=new ArrayList<>();
        /*获取表名*/
        ResultSet resultSet=databaseMetaData.getTables(schema,null,null,new String[]{"Table"});
        /*存储数据库名*/
        session.setAttribute("databasename",schema);
        System.out.println("Schemas="+schema);
        while(resultSet.next())
        {
            table_list.add(resultSet.getString(3));
            System.out.println("表名:"+resultSet.getString(3));
        }
        session.setAttribute("table_list",table_list);
        return "table";
    }

    /*读取文件*/
    @RequestMapping(value = "/updateFile", method = RequestMethod.POST)
    public String updateFile(MultipartFile file, HttpSession session) throws Exception {
        //清空session
        session.removeAttribute("failStr");
        session.removeAttribute("errorPrompt");
        System.out.println("执行updateFile方法");
        //存放取txt文件中内容；
        String txtStr = "";
        try {
            //获取上传文件名称
            String fileName = file.getOriginalFilename();
            //文件保存路径,getCanonicalPath()获取文件路径
            String targetFilePath = new File("./src/main").getCanonicalPath();
            File targetFile = new File(targetFilePath + File.separator + fileName);
            //保存文件
            file.transferTo(targetFile);
            System.out.println("添加成功");


            String encoding = "UTF-8";
            //获取文件路径
            File fl = new File(String.valueOf(targetFile));
            if (fl.isFile() && fl.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(fl), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                //按行输出txt文件内容
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    txtStr = txtStr + lineTxt;
                    System.out.println(lineTxt);
                }
                //调用newQuery方法执行txt文件
                newQuery(txtStr, session);
                read.close();
                //删除上传的文件
                targetFile.delete();
                System.out.println("删除文件成功");

            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            session.setAttribute("failStr", "执行文件失败");
            session.setAttribute("errorPrompt", e);
            e.printStackTrace();
        }
        return "newQuery";
    }



    /*新建查询*/
    @RequestMapping(value = "query", method = RequestMethod.POST)
    public String newQuery(String sql, HttpSession session) {

        System.out.println("开始执行此方法");
        //清空session
        session.removeAttribute("tableName");
        session.removeAttribute("fieldName");
        session.removeAttribute("pojolist");
        session.removeAttribute("modifyStr");
        session.removeAttribute("failStr");
        session.removeAttribute("errorPrompt");


        List<List<String>> pojolist = new ArrayList<>();//存储表内数据

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String modifyStr = "修改成功";
        String failStr = "查询失败";

        try {
            //1.注册驱动
            String driver = "com.mysql.cj.jdbc.Driver";
            Class.forName(driver);
            //2.数据库连接
            String url = "jdbc:mysql://"+session.getAttribute("Address").toString()+"/"+session.getAttribute("databasename").toString()+"?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
            String username = session.getAttribute("databaseuser").toString();
            String password = session.getAttribute("databasepsw").toString();
            conn = DriverManager.getConnection(url, username, password);

            //3.获取数据库操作对象
            stmt = conn.createStatement();

            // 4.执行sql语句
            //按分号分割字符串
            String[] temp = sql.split(";");
            //默认影响的行数
            int updateCount = -1;

            for (int i = 0; i < temp.length; i++) {

                //判断结尾是否存在一个空行
                if (temp[i].length() == 2) {
                    continue;
                }
                System.out.println("执行sql语句" + temp[i]);
                sql = temp[i];
                boolean hasResultSet = stmt.execute(sql);
                //执行sql，返回boolean值表示是否包含ResultSet
                if (hasResultSet) {
                    //如果执行后有ResultSet结果集
                    //获取结果集
                    rs = stmt.getResultSet();
                    //ResultSetMetaData分析结果集的元数据接口
                    ResultSetMetaData metaData = rs.getMetaData();

                    //获取数据库名
                    String DBName = metaData.getCatalogName(1);
                    System.out.println("数据库名:" + DBName);
                    //获取表名
                    String tableName = DBName + "_" + metaData.getTableName(1);
                    System.out.println("表名:" + tableName);

                    //获取列名
                    List<String> fieldName = new ArrayList<>();
                    System.out.println("列名：");
                    //获取列数
                    int columnCount = metaData.getColumnCount();
                    //存放字段名
                    for (int k = 1; k <= columnCount; k++) {
                        String columnName = metaData.getColumnName(k);
                        fieldName.add(columnName);
                        System.out.println(columnName);
                    }
                    System.out.println(fieldName);

                    //存放表内数据
                    while (rs.next()) {
                        List<String> pojoString = new ArrayList<>();
                        //输出ResultSet对象
                        for (int j = 0; j < columnCount; j++) {
                            System.out.println(rs.getString(j + 1));
                            pojoString.add(rs.getString(j + 1));
                        }
                        pojolist.add(pojoString);
                        System.out.println(pojoString);
                    }
                    System.out.println(pojolist);
                    //发送数据
                    session.setAttribute("tableName", tableName);
                    session.setAttribute("fieldName", fieldName);
                    session.setAttribute("pojolist", pojolist);
                } else {
                    updateCount = stmt.getUpdateCount();
                    System.out.println("该SQL语句影响的记录有" + updateCount + "条");
                }

            }
            if (updateCount >= 0) {
                //发送‘修改成功’字段
                session.setAttribute("modifyStr", modifyStr);
                System.out.println(updateCount);
            }

        } catch (Exception e) {
            //程序异常输出’查询失败‘字段
            session.setAttribute("failStr", failStr);
            session.setAttribute("errorPrompt", e);
            e.printStackTrace();
        } finally {
            //6.释放资源
            //为了保证资源一定释放，在finally语句块中关闭资源
            //并且遵循从小到大依次关闭，分别对其try..catch...
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return "newQuery";

    }
/*新建查询*/
//@RequestMapping(value = "query", method = RequestMethod.POST)
//public String newQuery(String sql,HttpSession session) throws ClassNotFoundException {
//
//    System.out.println("开始执行此方法");
//    //清空session
//    session.removeAttribute("tableName");
//    session.removeAttribute("fieldName");
//    session.removeAttribute("pojolist");
//    session.removeAttribute("modifyStr");
//    session.removeAttribute("failStr");
//    String sql1=sql;
//    session.setAttribute("sql",sql1);
//
//        return "newQuery";
//    }
//}
//
//
//    public void link(String sql,HttpSession session) {
//        String sql1=sql ;
//        List<List<String>> pojolist = new ArrayList<>();//存储表内数据
//        Connection conn = null;
//        Statement stmt = null;
//        ResultSet rs = null;
//        String modifyStr = "修改成功";
//        String failStr = "查询失败";
//        try {
//            //1.注册驱动
////            String driver = "com.mysql.cj.jdbc.Driver";
////            Class.forName(driver);
////            //2.数据库连接
////            String url = "jdbc:mysql://"+session.getAttribute("Address").toString()+"/"+session.getAttribute("databasename").toString()+"?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
//            String username = session.getAttribute("databaseuser").toString();
//            String password = session.getAttribute("databasepsw").toString();
//            conn = DriverManager.getConnection(url, username, password);
//
//            //3.获取数据库操作对象
//            stmt = conn.createStatement();
//
//            // 4.执行sql语句
//            //按分号分割字符串
//            String[] temp = sql.split(";");
//            //默认影响的行数
//            int updateCount = -1;
//
//            for (int i = 0; i < temp.length; i++) {
//
//                //判断结尾是否存在一个空行
//                if (temp[i].length() == 2) {
//                    continue;
//                }
//                System.out.println("执行sql语句" + temp[i]);
//                sql = temp[i];
//                boolean hasResultSet = stmt.execute(sql);
//                //执行sql，返回boolean值表示是否包含ResultSet
//                if (hasResultSet) {
//                    //如果执行后有ResultSet结果集
//                    //获取结果集
//                    rs = stmt.getResultSet();
//                    //ResultSetMetaData分析结果集的元数据接口
//                    ResultSetMetaData metaData = rs.getMetaData();
//
//                    //获取数据库名
//                    String DBName = metaData.getCatalogName(1);
//                    System.out.println("数据库名:" + DBName);
//                    //获取表名
//                    String tableName = DBName + "_" + metaData.getTableName(1);
//                    System.out.println("表名:" + tableName);
//
//                    //获取列名
//                    List<String> fieldName = new ArrayList<>();
//                    System.out.println("列名：");
//                    //获取列数
//                    int columnCount = metaData.getColumnCount();
//                    //存放字段名
//                    for (int k = 1; k <= columnCount; k++) {
//                        String columnName = metaData.getColumnName(k);
//                        fieldName.add(columnName);
//                        System.out.println(columnName);
//                    }
//                    System.out.println(fieldName);
//
//                    //存放表内数据
//                    while (rs.next()) {
//                        List<String> pojoString = new ArrayList<>();
//                        //输出ResultSet对象
//                        for (int j = 0; j < columnCount; j++) {
//                            System.out.println(rs.getString(j + 1));
//                            pojoString.add(rs.getString(j + 1));
//                        }
//                        pojolist.add(pojoString);
//                        System.out.println(pojoString);
//                    }
//                    System.out.println(pojolist);
//                    //发送数据
//                    session.setAttribute("tableName", tableName);
//                    session.setAttribute("fieldName", fieldName);
//                    session.setAttribute("pojolist", pojolist);
//                } else {
//                    updateCount = stmt.getUpdateCount();
//                    System.out.println("该SQL语句影响的记录有" + updateCount + "条");
//                }
//
//            }
//            if (updateCount >= 0) {
//                //发送‘修改成功’字段
//                session.setAttribute("modifyStr", modifyStr);
//                System.out.println(updateCount);
//            }
//
//        } catch (Exception e) {
//            //程序异常输出’查询失败‘字段
//            session.setAttribute("failStr", failStr);
//            session.setAttribute("errorPrompt", e);
//            e.printStackTrace();
//        } finally {
//            //6.释放资源
//            //为了保证资源一定释放，在finally语句块中关闭资源
//            //并且遵循从小到大依次关闭，分别对其try..catch...
//            if (rs != null) {
//                try {
//                    rs.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            if (stmt != null) {
//                try {
//                    stmt.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            if (conn != null) {
//                try {
//                    conn.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//    }



    /*编辑表*/
    @RequestMapping("/edittable")
    public  String edittable(String tablevalue,HttpSession session,boolean  keep,boolean add) throws SQLException, ClassNotFoundException {


        //存储选定表的列名
        List<String> edittablecolumn=new ArrayList<>();
        //存储表内数据
        List<List<String>> tablevaluelist=new ArrayList<>();
        System.out.println(tablevalue);
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //用户连接信息
        //String url="jdbc:mysql://"+Address+"/"+DataBaseName+"?serverTimezone=UTC&characterEncoding=utf-8";
        String url="jdbc:mysql://"+session.getAttribute("Address")+"/"+session.getAttribute("databasename")+"?serverTimezone=UTC&characterEncoding=utf-8";
        //获取连接
        Connection connection=DriverManager.getConnection(url, session.getAttribute("databaseuser").toString(), session.getAttribute("databasepsw").toString());
        //获取数据库操作对象
        Statement statement=connection.createStatement();

        session.setAttribute("tablevalue",tablevalue);
        //获取主键
        ResultSet resultSet1=connection.getMetaData().getPrimaryKeys(connection.getCatalog().toUpperCase(),null,session.getAttribute("tablevalue").toString().toUpperCase());
        while(resultSet1.next())
        {
            System.out.println("主键为："+resultSet1.getString("COLUMN_NAME"));
            session.setAttribute("PrimaryKey",resultSet1.getString("COLUMN_NAME"));
        }


        //设置SQL
        String sql="select * from "+tablevalue;
        //查询结果集
        ResultSet resultSet=statement.executeQuery(sql);
        ResultSetMetaData resultSetMetaData=resultSet.getMetaData();
        //获取列名
        System.out.println("列名：");
        int colCount = resultSetMetaData.getColumnCount();  //获取列数
        int PrimaryKeycol=-1;//存储主键的列数
        for (int i = 1; i <= colCount; i++) {
            String columnName = resultSetMetaData.getColumnName(i);
            if(columnName.equals(session.getAttribute("PrimaryKey").toString()))
            {
                 PrimaryKeycol=i;
            }
            edittablecolumn.add(columnName);
            System.out.println(columnName);
        }
        session.setAttribute("PrimaryKeycol",PrimaryKeycol);
        System.out.println("一共有:"+colCount+"列");
        while(resultSet.next())
        {
            List<String> tablevaluestring=new ArrayList<>();
            for (int i = 1; i <= colCount; i++) {
                tablevaluestring.add(resultSet.getString(i));
            }
            if(add)
            {
               for(int i=1;i<=colCount;i++)
               {
                   tablevaluestring.add(" ");
               }
            }
            tablevaluelist.add(tablevaluestring);
        }
        System.out.println("add="+add);
        System.out.println("keep="+keep);
        //session存储
        session.setAttribute("colCount",colCount);
        session.setAttribute("edittablename",tablevalue);
        session.setAttribute("edittablecolumn",edittablecolumn);
        session.setAttribute("tablevaluelist",tablevaluelist);
        return "tabledata";
    }

    /*添加行*/
    @RequestMapping(value="addvalue",method = RequestMethod.POST)
    public String addvalue(HttpSession session,String... arr) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //用户连接信息
        //String url="jdbc:mysql://"+Address+"/"+DataBaseName+"?serverTimezone=UTC&characterEncoding=utf-8";
        String url="jdbc:mysql://"+session.getAttribute("Address")+"/"+session.getAttribute("databasename")+"?serverTimezone=UTC&characterEncoding=utf-8";
        //获取连接
        Connection connection=DriverManager.getConnection(url, session.getAttribute("databaseuser").toString(), session.getAttribute("databasepsw").toString());
        //获取数据库操作对象
        Statement statement=connection.createStatement();
        //设置SQL
        String sql="insert into "+session.getAttribute("tablevalue").toString()+" values( ";
        for(int i=0;i<arr.length-1;i++)
        {
            sql+="'"+arr[i]+"',";
            System.out.println("arr:"+arr[i]);
        }
        sql+="'"+arr[arr.length-1]+"')";
        System.out.println("执行的sql语句为："+sql);
        int index=statement.executeUpdate(sql);
        if(index>0)
        {
            System.out.println("执行成功！");

            //记录日志
            Operationlog op_log=new Operationlog();
            User user=(User) session.getAttribute("user");
            op_log.setEmail(user.getEmail());
            op_log.setDatabasename(session.getAttribute("databasename").toString());
            op_log.setOperation(sql);
            op_log.setOperationdate(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            this.iOperationlogService.save(op_log);
        }
        else {
            System.out.println("执行失败！");
        }
        String path="redirect:/usercreatetable/edittable?tablevalue="+session.getAttribute("tablevalue").toString();

        return path;
    }

    /*删除数据*/
    @RequestMapping("deletevalue")
    public String deletebalue(HttpSession session,@RequestParam List<String> list) throws ClassNotFoundException, SQLException {

        System.out.println("删除的信息为："+list);
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //用户连接信息
        //String url="jdbc:mysql://"+Address+"/"+DataBaseName+"?serverTimezone=UTC&characterEncoding=utf-8";
        String url="jdbc:mysql://"+session.getAttribute("Address")+"/"+session.getAttribute("databasename")+"?serverTimezone=UTC&characterEncoding=utf-8";
        //获取连接
        Connection connection=DriverManager.getConnection(url, session.getAttribute("databaseuser").toString(), session.getAttribute("databasepsw").toString());
        //获取数据库操作对象
        Statement statement=connection.createStatement();
        //设置sql
        String sql="delete from "+session.getAttribute("tablevalue")+" where ";
        System.out.println(session.getAttribute("edittablecolumn").toString());
        String colname=session.getAttribute("edittablecolumn").toString();
        //去掉[]
        colname=colname.substring(1,colname.length()-1);
        String[] colname1=colname.split(",");
        for(int i=0;i<colname1.length-1;i++)
        {
            sql+=colname1[i]+"='"+list.get(i)+"' and ";
        }
        sql+=colname1[colname1.length-1]+"='"+list.get(colname1.length-1)+"'";
        System.out.println("运行的sql语句为："+sql);
        int index=statement.executeUpdate(sql);
        if(index>0)
        {
            System.out.println("执行成功！");

            //记录日志
            Operationlog op_log=new Operationlog();
            User user=(User) session.getAttribute("user");
            op_log.setEmail(user.getEmail());
            op_log.setDatabasename(session.getAttribute("databasename").toString());
            op_log.setOperation(sql);
            op_log.setOperationdate(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            this.iOperationlogService.save(op_log);
        }
        else {
            System.out.println("执行失败！");
        }

        String path="redirect:/usercreatetable/edittable?tablevalue="+session.getAttribute("tablevalue").toString();
        return path;
    }

    /*修改数据*/
    @RequestMapping(value = "updatevalue",method = RequestMethod.POST)
    public String updatevalue(HttpSession session,String...arr) throws ClassNotFoundException, SQLException {
        //System.out.println("修改的信息为："+list);
        for(int i=0;i<arr.length;i++)
        {
            System.out.println("需要修改："+arr[i]);
        }
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //用户连接信息
        //String url="jdbc:mysql://"+Address+"/"+DataBaseName+"?serverTimezone=UTC&characterEncoding=utf-8";
        String url="jdbc:mysql://"+session.getAttribute("Address")+"/"+session.getAttribute("databasename")+"?serverTimezone=UTC&characterEncoding=utf-8";
        //获取连接
        Connection connection=DriverManager.getConnection(url, session.getAttribute("databaseuser").toString(), session.getAttribute("databasepsw").toString());
        //获取数据库操作对象
        Statement statement=connection.createStatement();
        //设置sql
        String sql="update "+session.getAttribute("tablevalue").toString()+" set ";
        String colname=session.getAttribute("edittablecolumn").toString();
        //去掉[]
        colname=colname.substring(1,colname.length()-1);
        //以逗号分割
        String[] colname1=colname.split(",");

        //记录日志
        String op_str="";

        for(int i=0;i<colname1.length-1;i++)
        {
            if(i==Integer.parseInt(session.getAttribute("PrimaryKeycol").toString())-1) continue;
            sql+=colname1[i]+"='"+arr[i]+"',";

            op_str+=colname1[i]+"='"+arr[i]+"',";

            System.out.println(Integer.parseInt(session.getAttribute("PrimaryKeycol").toString()));
            System.out.println("当前添加列数为："+i);
        }

        op_str+=colname1[colname1.length-1]+"='"+arr[colname1.length-1];

        sql+=colname1[colname1.length-1]+"='"+arr[colname1.length-1]+"' where "+session.getAttribute("PrimaryKey").toString()+" = '"+arr[Integer.parseInt(session.getAttribute("PrimaryKeycol").toString())-1]+"'";
        System.out.println("运行的sql语句为："+sql);
        int index=statement.executeUpdate(sql);
        if(index>0)
        {
            System.out.println("执行成功！");

            //记录日志
            Operationlog op_log=new Operationlog();
            User user=(User) session.getAttribute("user");
            op_log.setEmail(user.getEmail());
            op_log.setDatabasename(session.getAttribute("databasename").toString());
            op_log.setOperation("把"+session.getAttribute("databasename")+"数据库"+session.getAttribute("tablevalue").toString()+"表中主键"+session.getAttribute("PrimaryKey").toString()+"为"+arr[Integer.parseInt(session.getAttribute("PrimaryKeycol").toString())-1]+"的数据修改为"+op_str);
            op_log.setOperationdate(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            this.iOperationlogService.save(op_log);

        }
        else {
            System.out.println("执行失败！");
        }
        String path="redirect:/usercreatetable/edittable?tablevalue="+session.getAttribute("tablevalue").toString();
        return path;

    }
    @RequestMapping(value = "/createtable", method = RequestMethod.POST)
    public String createTable(HttpSession session, String tablename, String... field) throws SQLException, ClassNotFoundException {

//        数据库连接
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        String url = "jdbc:mysql://" + session.getAttribute("Address").toString() + "/" + session.getAttribute("databasename").toString() + "?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
        String username = session.getAttribute("databaseuser").toString();
        String password = session.getAttribute("databasepsw").toString();
        Connection connection = DriverManager.getConnection(url, username, password);

        Statement statement = connection.createStatement();

        String createtablesql;

//        统计主键
        List keylist = new ArrayList();
        for (int i = 3; i < field.length; i += 5) {
            if (field[i].equals("1")) {
                keylist.add(i);
            }
        }

//        sql拼接
        createtablesql = "CREATE TABLE if not exists `" + tablename + "` (";
        for (int i = 0; i < field.length; i += 5) {
            createtablesql += "`" + field[i] + "` " + field[i + 1] + "(" + field[i + 2] + ") comment '" + field[i + 4] + "',";
        }

        if (keylist.size() != 0) {
            createtablesql += "PRIMARY KEY(";
            for (int i = 0; i < keylist.size(); i++) {
                createtablesql += "`" + field[(int) keylist.get(i)-3] + "`,";
            }
            createtablesql = createtablesql.substring(0, createtablesql.length() - 1);
            createtablesql += " )";
        } else {
            createtablesql = createtablesql.substring(0, createtablesql.length() - 1);
        }
        createtablesql += " );";


        List list = (List) session.getAttribute("table_list");

        if (list.indexOf(tablename) != -1) {
            System.out.println("数据表已存在");

        } else {
            statement.execute(createtablesql);
            System.out.println("数据库" + tablename + "创建成功");


            //记录日志
            Operationlog op_log=new Operationlog();
            User user=(User) session.getAttribute("user");
            op_log.setEmail(user.getEmail());
            op_log.setDatabasename(session.getAttribute("databasename").toString());
            op_log.setOperation("创建数据表"+tablename);
            op_log.setOperationdate(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            this.iOperationlogService.save(op_log);

        }

        statement.close();
        return "redirect:/index";
    }



}

